package com.education.springbootdemo.service;

import com.education.springbootdemo.domain.doto.Category;
import com.education.springbootdemo.exception.BizException;
import com.education.springbootdemo.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService{


     @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Category add(Category category) throws BizException{
        try{
            //想数据库中插入
            categoryMapper.addCategory(category);
        }catch (Exception e){
            throw new BizException(e.getMessage());
        }

        if(category.getId() == null || category.getId() == 0L) {
            throw new BizException("插入分类失败,分类ID为空");
        }

        //根据ID重新查询
        return this.get(category.getId());
    }

    @Override
    public Category get(Long id)  {
        return categoryMapper.queryById(id);
    }

    @Override
    public  List<Category> list() {
        List<Category> list = categoryMapper.queryAllCategory();
        return list;
    }
}
